package com.pet.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.pet.api.dto.AddressDTO;
import com.pet.common.domain.Result;
import com.pet.common.domain.UserContext;
import com.pet.user.domain.dto.AddressFormDTO;
import com.pet.user.domain.po.Address;
import com.pet.user.domain.vo.AddressVO;
import com.pet.user.mapper.AddressMapper;
import com.pet.user.service.IAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户收货地址表 服务实现类
 * </p>
 *
 * @author Cookies
 * @since 2024-07-03
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Override
    public List<AddressVO> getAddressesByUser() {
        Integer userId = UserContext.getUser();
        List<Address> addressList = lambdaQuery()
                .eq(Address::getUserId, userId)
                .list();
        List<AddressVO> addressVOList = BeanUtil.copyToList(addressList, AddressVO.class);
        return addressVOList;
    }

    @Override
    @Transactional
    public boolean addAddressByUser(AddressFormDTO addressFormDTO) {
        Integer userId = UserContext.getUser();
        Address address = BeanUtil.copyProperties(addressFormDTO, Address.class);
        address.setUserId(userId);
        // 若不设为默认地址则直接保存
        if (address.getIsDefault() == 0) {
            return save(address);
        }
        // 否则需要检查是否已有默认地址
        boolean isSucceed = checkAddressDefaultStatus(userId);
        return isSucceed ? save(address) : false;
    }

    @Override
    @Transactional
    public boolean updateAddressByUser(AddressFormDTO addressFormDTO) {
        Integer userId = UserContext.getUser();
        // 获取当前地址对应的用户id
        Integer addressUserId = lambdaQuery()
                .select(Address::getUserId)
                .eq(Address::getId, addressFormDTO.getId())
                .one().getUserId();
        // 若对应不上则视为危险行为, 拒绝执行
        if (!userId.equals(addressUserId)) {
            return false;
        }
        Address address = BeanUtil.copyProperties(addressFormDTO, Address.class);
        // 若不设为默认地址则直接更新
        if (address.getIsDefault() == 0) {
            return updateById(address);
        }
        // 否则需要检查是否已有默认地址
        boolean isSucceed = checkAddressDefaultStatus(userId);
        return isSucceed ? updateById(address) : false;
    }

    private boolean checkAddressDefaultStatus(Integer userId) {
        Address address = lambdaQuery()
                .eq(Address::getUserId, userId)
                .eq(Address::getIsDefault, 1)
                .one();
        if (address == null) {
            return true;
        }
        address.setIsDefault(0);
        return updateById(address);
    }

    @Override
    public boolean deleteAddressByUser(Integer id) {
        Integer userId = UserContext.getUser();
        // 检查是否存在该用户对应的地址
        Address address = lambdaQuery()
                .eq(Address::getId, id)
                .eq(Address::getUserId, userId)
                .one();
        if (address == null) {
            return false;
        }
        return removeById(id);
    }

    @Override
    public AddressDTO queryAddressById(Integer id) {
        Address address = getById(id);
        return BeanUtil.copyProperties(address, AddressDTO.class);
    }

    @Override
    public Result getAddressById(Integer id) {
        AddressVO addressVO = BeanUtil.copyProperties(getById(id), AddressVO.class);
        return Result.success(addressVO);
    }
}
